package com.fary.ibmmq;

import cn.hutool.core.collection.CollectionUtil;
import com.fary.config.MQConfigProperties;
import com.fary.config.MQConfigProperties.Source;
import com.fary.config.MQConfigProperties.Dest;
import com.fary.config.MQConfigProperties.Sender;
import com.fary.config.MQConfigProperties.Receiver;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class JmsConnectionFactoryProvider implements InitializingBean {

    @Autowired
    private MQConfigProperties mqConfigProperties;

    private final Map<String, Source> sourceMap = new HashMap<String, Source>();

    private final Map<String, Dest> destMap = new HashMap<String, Dest>();

    public Map<String, Source> getSourceMap() {
        return sourceMap;
    }

    public Map<String, Dest> getDestMap() {
        return destMap;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Sender sender = mqConfigProperties.getSender();
        if (sender != null && CollectionUtil.isNotEmpty(sender.getDestList())) {
            destMap.putAll(sender.getDestList().stream().collect(Collectors.toMap(Dest::getSysCode, dest -> dest)));
        }
        Receiver receiver = mqConfigProperties.getReceiver();
        if (receiver != null && CollectionUtil.isNotEmpty(receiver.getSourceList())) {
            sourceMap.putAll(receiver.getSourceList().stream().collect(Collectors.toMap(Source::getNodeFlag, source -> source)));
        }
    }

    /**
     * 获得jms连接工厂
     */
    public ConnectionFactory getConnectionFactory(String sysCode, String type) {
        if (type.equals(Constants.OP_TYPE_SEND)) {
            return getSendConnectionFactory(sysCode);
        }
        if (type.equals(Constants.OP_TYPE_RECV)) {
            return getReceiverConnectionFactory(sysCode);
        }
        return null;
    }

    /**
     * 获得发送的连接
     */
    public ConnectionFactory getSendConnectionFactory(String sysCode) {
        Sender sender = mqConfigProperties.getSender();
        if (CollectionUtil.isEmpty(destMap)) {
            throw new RuntimeException("未找发送队列的配置信息");
        }
        Dest dest = destMap.get(sysCode);
        MQQueueConnectionFactory sendFactory = new MQQueueConnectionFactory();
        try {
            //设置主机
            sendFactory.setHostName(sender.getHost());
            //设置端口
            sendFactory.setPort(sender.getPort());
            //设置服务端连接通道
            sendFactory.setChannel(dest.getChannel());
            //设置ccsid
            sendFactory.setCCSID(sender.getCcsId());
            //设置队列管理器
            sendFactory.setQueueManager(sender.getManager());
            //设置连接方式
            sendFactory.setTransportType(1);
            return sendFactory;
        } catch (JMSException ex) {
            ex.printStackTrace();
            System.out.println("创建连接失败");
        }
        return null;
    }


    public ConnectionFactory getReceiverConnectionFactory(String nodeFlag) {
        Receiver receiver = mqConfigProperties.getReceiver();
        if (CollectionUtil.isEmpty(sourceMap)) {
            throw new RuntimeException("未找接收队列的配置信息");
        }
        Source source = sourceMap.get(nodeFlag);
        MQQueueConnectionFactory receiverFactory = new MQQueueConnectionFactory();
        try {
            //设置主机
            receiverFactory.setHostName(source.getHost());
            //设置端口
            receiverFactory.setPort(source.getPort());
            //设置服务端连接通道
            receiverFactory.setChannel(source.getChannel());
            //设置ccsid
            receiverFactory.setCCSID(source.getCcsId());
            //设置队列管理器
            receiverFactory.setQueueManager(source.getManager());
            //设置连接方式
            receiverFactory.setTransportType(1);
            return receiverFactory;
        } catch (JMSException ex) {
            ex.printStackTrace();
            System.out.println("创建连接失败");
        }
        return null;
    }

}
